package com.samsung.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.samsung.sbackup.R;

public class AppsAdapter extends BaseAdapter {

	Context context;
	public ArrayList<ApplicationInfo> infos;
	boolean[] checkedItems;
	int resourceLayout;
	SparseArray<Drawable> hashMapIcon;
	SparseArray<String> hashMapName;

	public AppsAdapter(Context ctx, ArrayList<ApplicationInfo> infos,
			int resourceLayout, SparseArray<Drawable> hashMapIcon,
			SparseArray<String> hashMapName, boolean[] checkedItems) {
		this.context = ctx;
		this.infos = infos;
		this.resourceLayout = resourceLayout;
		this.hashMapIcon = hashMapIcon;
		this.hashMapName = hashMapName;
		if (checkedItems == null)
			this.checkedItems = new boolean[this.infos.size()];
		else
			this.checkedItems = checkedItems;
	}

	public ArrayList<ApplicationInfo> getSelectedItems() {
		ArrayList<ApplicationInfo> selected = new ArrayList<ApplicationInfo>();
		for (int i = 0; i < checkedItems.length; i++) {
			if (checkedItems[i])
				selected.add(infos.get(i));
		}
		return selected;
	}

	public boolean[] getCheckedItems() {
		return checkedItems;
	}

	public void setCheckedItems(boolean[] checkedItems) {
		this.checkedItems = checkedItems;
		notifyDataSetChanged();
	}

	public void addItem(ApplicationInfo f) {
		infos.add(f);
		notifyDataSetChanged();
	}

	public void clear() {
		infos.clear();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return infos.size();
	}

	@Override
	public ApplicationInfo getItem(int arg0) {
		return infos.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		{
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(resourceLayout, parent, false);
			holder = new ViewHolder(convertView);
		}

		holder.populateFrom(getItem(position), position);
		return convertView;
	}

	class ViewHolder {
		ImageView imgFile;
		TextView fileName;
		CheckBox cb;

		public ViewHolder(View row) {
			imgFile = (ImageView) row.findViewById(R.id.app_item_iv);
			fileName = (TextView) row.findViewById(R.id.app_item_tv);
			cb = (CheckBox) row.findViewById(R.id.app_item_cb);
		}

		public void populateFrom(ApplicationInfo app, final int position) {
			fileName.setText(hashMapName.get(position));
			imgFile.setImageDrawable(hashMapIcon.get(position));

			cb.setChecked(checkedItems[position]);

			cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					checkedItems[position] = isChecked;
				}
			});
		}
	}
}
